import React from 'react';
import {Table} from 'antd';
import PropTypes from 'prop-types';
import routes from './routes';
import {TABLE, PAGINATION_OPTIONS, FIXED_COLUMN_WIDTH, COMMON_TABLE_QUERY_FAIL_TEXT, COMMON_TABLE_EMPTY_TEXT} from '../constants';
import {Link} from 'react-router-dom';
import {hasPermissions} from './utils';
import {baseDataStatus, agentKind} from '../Enum';
import {conventEnumValueToString} from '../utils';
import {PERMISSIONS} from './constants';
import DropdownMenu from 'Shared/components/DropdownMenu';
import {injectIntl} from './intl';
export class TablePanel extends React.PureComponent {
    constructor(props) {
        super(props);
        this.state = {
            loading: false,
        };
    }

    onChangeSort = (pagination, filters, sorter) => {
        this.props.onChangeSort({
            pageIndex: pagination.current - 1,
            pageSize: pagination.pageSize,
            sortedColumnKey: sorter.columnKey,
            sortedOrder: sorter.order,
        });
    };
    render() {
        const {pageIndex, pageSize, total, sortedColumnKey, sortedOrder} = this.props;
        const paginationInfo = {
            total,
            pageSize,
            current: pageIndex + 1,
            ...PAGINATION_OPTIONS
        };
        const locale = {
            emptyText: this.props.errorMessage ? COMMON_TABLE_QUERY_FAIL_TEXT : COMMON_TABLE_EMPTY_TEXT
        };

        const columns = [
            {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.code',
                    defaultMessage: '编号'
                }),
                dataIndex: 'code',
                sorter: true,
                sortOrder: sortedColumnKey === 'code' && sortedOrder,
                render: (text, record) => <Link to={routes.detail.format(record.id)} >{text}</Link>
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.name',
                    defaultMessage: '名称'
                }),
                dataIndex: 'name',
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.shortCode',
                    defaultMessage: '简码'
                }),
                dataIndex: 'shortCode',
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.shortName',
                    defaultMessage: '简称'
                }),
                dataIndex: 'shortName',
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.kind',
                    defaultMessage: '客户分类'
                }),
                dataIndex: 'kind',
                render: text => conventEnumValueToString(agentKind, text)
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.marketDepartmentCode',
                    defaultMessage: '大区编号'
                }),
                dataIndex: 'marketDepartmentCode',
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.country',
                    defaultMessage: '国家'
                }),
                dataIndex: 'country',
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.province',
                    defaultMessage: '省份'
                }),
                dataIndex: 'province',
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.city',
                    defaultMessage: '城市'
                }),
                dataIndex: 'city',
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.contactNumber',
                    defaultMessage: '联系电话'
                }),
                dataIndex: 'contactNumber',
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.address',
                    defaultMessage: '联系地址'
                }),
                dataIndex: 'address',
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.status',
                    defaultMessage: '状态'
                }),
                dataIndex: 'status',
                render: text => conventEnumValueToString(baseDataStatus, text)
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.action',
                    defaultMessage: '操作'
                }),
                dataIndex: 'action',
                width: FIXED_COLUMN_WIDTH,
                fixed: 'right',
                render: (t, r) => {
                    const menus = [
                        {
                            id: PERMISSIONS.update,
                            children: (
                                <Link key="update" to={routes.update.format(r.id)}>
                                    {this.props.intl.formatMessage({
                                        id: 'tablePanel.update',
                                        defaultMessage: '编辑'
                                    })}
                                </Link>
                            ),
                            hidden: !(hasPermissions(this.props.permission, PERMISSIONS.update) && hasPermissions(r.options, PERMISSIONS.update))
                        }
                    ];
                    return <DropdownMenu key={r.id} menus={menus} primaryLength={1} id={r.id} />;
                }
            }
        ];
        return (
            <div>
                <Table
                    className="white-space-nowrap"
                    columns={columns}
                    dataSource={this.props.data}
                    loading={this.props.loading || this.state.loading}
                    onChange={this.onChangeSort}
                    pagination={paginationInfo}
                    rowKey="id"
                    locale={locale}
                    {...TABLE} >
                </Table>
            </div>
        );
    }
}

TablePanel.propTypes = {
    data: PropTypes.array.isRequired,
    errorMessage: PropTypes.string.isRequired,
    loading: PropTypes.bool.isRequired,
    permission: PropTypes.array.isRequired,
    total: PropTypes.number.isRequired,
    onChangeSort: PropTypes.func.isRequired,
    intl: PropTypes.object,
    pageIndex: PropTypes.number,
    pageSize: PropTypes.number,
    refreshListData: PropTypes.func,
    sortedColumnKey: PropTypes.string,
    sortedOrder: PropTypes.string,
};

import {connect} from 'react-redux';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
import {
    tableSearch,
} from './actions';
const getData = selectorFactory(['page', 'domainData', 'list', 'data']);
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);
const mapStateToProps = state => ({
    data: getData(state),
    loading: state.getIn(['page', 'domainData', 'list', 'isFetching']),
    total: state.getIn(['page', 'domainData', 'list', 'total']),
    errorMessage: state.getIn(['page', 'domainData', 'list', 'message']),
    pageIndex: state.getIn(['page', 'appState', 'queryBySort', 'pageIndex']),
    pageSize: state.getIn(['page', 'appState', 'queryBySort', 'pageSize']),
    sortedColumnKey: state.getIn(['page', 'appState', 'queryBySort', 'sortedColumnKey']),
    sortedOrder: state.getIn(['page', 'appState', 'queryBySort', 'sortedOrder']),
    permission: getPermissions(state),
});
const mapDispatchToProps = dispatch => ({
    onChangeSort: option => dispatch(tableSearch(option)),
    refreshListData: () => dispatch(tableSearch()),
});

export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(TablePanel));

